package com.spring.activity;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import com.spring.httputils.RequestParams;
import com.spring.httputils.RestClient;
import com.spring.httputils.TextHttpResponseHandler;
import com.spring.tool.App;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

/**
 * 自助挂失
 * @author Administrator
 *
 */
public class  LockCard2Activity extends BaseActivity{
	RestClient restClient = null;
	private String username,logintype;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		SharedPreferences userInfo = getSharedPreferences(
				"user_info", 0);
		username = userInfo.getString("username", "");
		logintype = userInfo.getString("logintype", "");
		restClient = new RestClient(LockCard2Activity.this);
		SendHttpData sendhttpdata = new SendHttpData();
		sendhttpdata.run();
		
	}
	class SendHttpData implements Runnable {

		@Override
		public void run() {
			String url = App.getInstance().url+"/LockCard";
			RequestParams requestparams = new RequestParams(url, "POST");
			requestparams.put("LoginAccount", username);
			requestparams.put("LoginType", logintype);
			requestparams.put("CardStatus", "1");
			TextHttpResponseHandler textHttpResponseHandler = new TextHttpResponseHandler() {
				@Override
				public void onSuccess(int statusCode, Header[] headers,
						String responseString) {
					// TODO Auto-generated method stub
					Log.e("TAG", "responseString---" + responseString);
					try {
						AnalysisData(responseString);
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}

				@Override
				public void onFailure(int statusCode, Header[] headers,
						String responseString, Throwable throwable) {
					// TODO Auto-generated method stub
					Log.e("TAG", "responseString---" + responseString);

				}
			};
			try {
				restClient.post(url, requestparams, textHttpResponseHandler);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		/**
		 * json数据解析
		 * 
		 * @param strResult
		 * @throws JSONException
		 */
		private void AnalysisData(String strResult) throws JSONException {
			/**
			 * 这里需要分析服务器回传的json格式数据，
			 */
			JSONObject jsonObject = new JSONObject(strResult);
			try {
				String returnMsg = jsonObject.getString("returnMsg");
				if (returnMsg == null || "".equals(returnMsg)) {
					Toast.makeText(LockCard2Activity.this, "自助解挂成功", 2).show();
				} else {
					Toast.makeText(LockCard2Activity.this, returnMsg, 2).show();
				}
				Intent intent = new Intent(LockCard2Activity.this,
						MainActivity.class);
				startActivity(intent);
				finish();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		//

	}
}
